﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using BioBooking.Data;
using System.ComponentModel;
using System.Collections.ObjectModel;

namespace BioBooking.Client.Screens
{
    /// <summary>
    /// Interaction logic for Showing.xaml
    /// </summary>
    public partial class CreateShowing : UserControl
    {
        BioBooking.Data.IRepository serviceC;
        ObservableCollection<Cinema> cinemas;
        ObservableCollection<Movie> movies;
        ObservableCollection<Showing> showings;
        Movie selectedMovies;
        Showing selectedShowings;

        public CreateShowing()
        {
            InitializeComponent();
            
            if (!DesignerProperties.GetIsInDesignMode(this))
            {
                serviceC = Repository.GetRepository();

                movies = new ObservableCollection<Movie>(serviceC.ListAllActiveMovies());
                lbMovies.DataContext = movies;
                showings = new ObservableCollection<Showing>(serviceC.GetAllShowings());
                lbShowings.DataContext = showings;
                cinemas = new ObservableCollection<Cinema>(serviceC.GetCinemas());
                cbCinima.DataContext = cinemas;

                loadStartTime();
            }

        }

        private void Grid_Loaded(object sender, RoutedEventArgs e)
        {
        }

        private void btnCreateShowing_Click(object sender, RoutedEventArgs e)
        {
            Movie mov = (lbMovies.SelectedItem as Movie);
            if (mov == null)
                return;

            DateTime startTime = Convert.ToDateTime(cbStartDate.SelectedValue);
            DateTime endTime = startTime.AddMinutes(mov.Runtime);
            Cinema cin = (cbCinima.SelectedItem as Cinema);

            //Showing showing = new Showing() { Cinema = cin, Date = startTime.Date, StartTime = startTime, EndTime = endTime, Movie = mov };

            //serviceC.CreateShowingObject(showing);
            serviceC.CreateShowing(startTime, endTime, Convert.ToDecimal(txtPrice.Text), startTime, mov, cin);
        }

        private void loadStartTime()
        {
            cbStartDate.Items.Clear();
            
            for (int i = 1; i <= 48; i++)
            {
                cbStartDate.Items.Add(DateTime.Today.AddHours(i*0.5).ToShortTimeString());
            }
        }

        private void lbMovies_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            Movie movie = lbMovies.SelectedItem as Movie;
            if (movie == null)
                return;

            selectedMovies = movie;
        }

        private void lbShowings_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            Showing showing = lbShowings.SelectedItem as Showing;
            if (showing == null)
            {
                return;
            }
            selectedShowings = showing;
        }
    }
}
